package net.xdclass.mq;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.model.OrderMessage;
import net.xdclass.service.ProductOrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author : zhen.xue
 * @date : 2021-06-30 22:38
 */
@Slf4j
@Component
@RabbitListener(queues = {"${mqconfig.order_close_queue}"})
public class ProductOrderMQLisenter {

    @Autowired
    private ProductOrderService orderService;

    @RabbitHandler
    public void closeProductOrder(OrderMessage orderMessage, Message message, Channel channel){
        log.info("监听到确认订单消息: closeProductOrder:{}",orderMessage);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            boolean flag = orderService.closeProductOrder(orderMessage);
            if(flag){
                channel.basicAck(deliveryTag,false);
            }else{
                // 重新入队
                channel.basicReject(deliveryTag,true);
            }
        } catch (Exception exception) {
            log.error("定时关单失败:{}",orderMessage);
        }

    }
}